<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
    <meta charset="UTF-8">
</head>
<body>
<h2>登录</h2>
<form id="loginForm">
    <label for="username">用户名：</label>
    <input type="text" id="username" name="username" required><br><br>
    <label for="password">密码：</label>
    <input type="password" id="password" name="password" required><br><br>
    <button type="submit">登录</button>
</form>
<script>
    document.getElementById('loginForm').addEventListener('submit', function(event) {
        event.preventDefault();
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        // 构建请求参数
        const params = new URLSearchParams();
        params.append('username', username);
        params.append('password', password);

        fetch('/api/login', {
            method: 'POST',
            body: params // 使用构建的参数
        })
            .then(response => response.text())
            .then(data => {
                alert(data);
                if(data.includes("登录成功")) {
                    window.location.href = 'faq.html?userId=' + username;
                }
            });
    });
</script>
</body>
</html>